<%@ page import="gfo.Bracket; gfo.Player" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta name="layout" content="main"/>
    <g:set var="entityName" value="${message(code: 'player.label', default: 'Player')}"/>
    <title><g:message code="default.show.label" args="[entityName]"/></title>
</head>

<body>

<div class="body">
    <h1><g:message code="default.show.label" args="[entityName]"/>
        <span class="menuButton"><g:link class="create" action="create">New Player</g:link></span></h1>
    <g:if test="${flash.message}">
        <div class="message">${flash.message}</div>
    </g:if>
    <div class="about"><h1><g:link action="show" id="${playerInstance.id}">${playerInstance.fullName}</g:link></h1>
    ${playerInstance.description.encodeAsHTML()}</div>
    <div class="record"><h2>Record</h2>
        <div class="totalRecord">
        <h3>Total:</h3>
        <table>
            <tbody>
            <g:each in="${0..Bracket.list().size()+1}" var="i">
                <tr class="results">
                    <g:each in="${0..3}" var="j">
                        <g:if test="${!(i==0 && j==0)}">
                            <g:if test="${i==0}"><!--wins/losses/total--make a method??-->
                                <td>${playerInstance.showGrid()[j-1]}</td>
                            </g:if>
                            <g:elseif test="${i==5}"><!--totals row--shaded?-->
                                <g:if test="${j==0}">
                                    <td>Totals</td>
                                </g:if>
                                <g:else>
                                    <td>${playerInstance.calcWins()[j-1]}</td>
                                </g:else>
                            </g:elseif>
                            <g:else>
                                <g:if test="${j==0}">
                                    <td>${Bracket.list()[i-1]}</td>
                                </g:if>
                                <g:else><!--figure out a way to implement this logic-->
                                    <td>${playerInstance.calcWinsByBracket(Bracket.list()[i-1])[j-1]}</td>
                                </g:else>
                            </g:else>
                        </g:if>
                        <g:else><td></td></g:else>
                    </g:each>
                </tr>
            </g:each>
            </tbody>
        </table>
        </div>
        
        <div class="charRecord">
            <table>
                <thead>
                <tr>
                    <td>Character</td>
                    <td>Wins</td>
                    <td>Losses</td>
                    <td>Total</td>
                </tr>
                </thead>
                <tbody>
                <g:each in="${playerInstance.charRoster(playerInstance.matches.match)}" var="character">
                    <tr>
                        <td><g:link controller="fighter" action="show" id="${character.id}">
                            ${character}
                        </g:link></td>
                        <td>${playerInstance.calcWinsByFighter(character)[0]} /
                            ${playerInstance.calcWinsByFighter(character)[0]/playerInstance.calcWinsByFighter(character)[2]}</td>
                        <td>${playerInstance.calcWinsByFighter(character)[1]}</td>
                        <td>${playerInstance.calcWinsByFighter(character)[2]}</td>
                    </tr>
                </g:each>
                </tbody>
            </table>
        </div>

        <div class="oppCharRecord">
            <table>
                <thead>
                <tr>
                    <td>Opponent</td>
                    <td>Wins</td>
                    <td>Losses</td>
                    <td>Total</td>
                </tr>
                </thead>
                <tbody>
                <g:each in="${playerInstance.oppCharRoster(playerInstance.matches.match)}" var="character">
                    <tr>
                        <td><g:link controller="fighter" action="show" id="${character.id}">
                            ${character}
                        </g:link></td>
                        <td>${playerInstance.calcWinsByOppFighter(character)[0]} /
                            ${playerInstance.calcWinsByOppFighter(character)[0]/playerInstance.calcWinsByOppFighter(character)[2]}</td>
                        <td>${playerInstance.calcWinsByOppFighter(character)[1]}</td>
                        <td>${playerInstance.calcWinsByOppFighter(character)[2]}</td>
                    </tr>
                </g:each>
                </tbody>
            </table>
        </div>

        <div class="stageRecord">
            <table>
                <thead>
                <tr>
                    <td>Stage</td>
                    <td>Wins</td>
                    <td>Losses</td>
                    <td>Total</td>
                </tr>
                </thead>
                <tbody>
                <g:each in="${playerInstance.stageRoster(playerInstance.matches.match)}" var="stage">
                    <tr>
                        <td><g:link controller="stage" action="show" id="${stage.id}">
                            ${stage}
                        </g:link></td>
                        <td>${playerInstance.calcWinsByStage(stage)[0]} /
                            ${playerInstance.calcWinsByStage(stage)[0]/playerInstance.calcWinsByStage(stage)[2]}</td>
                        <td>${playerInstance.calcWinsByStage(stage)[1]}</td>
                        <td>${playerInstance.calcWinsByStage(stage)[2]}</td>
                    </tr>
                </g:each>
                </tbody>
            </table>
        </div>
    </div>

    <div class="buttons">
        <g:form>
            <g:hiddenField name="id" value="${playerInstance?.id}"/>
            <span class="button"><g:actionSubmit class="edit" action="edit"
                                                 value="${message(code: 'default.button.edit.label', default: 'Edit')}"/></span>
            <span class="button"><g:actionSubmit class="delete" action="delete"
                                                 value="${message(code: 'default.button.delete.label', default: 'Delete')}"
                                                 onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure?')}');"/></span>
        </g:form>
    </div>
    <h1>Matches
    </h1>
    <ul>
        <g:each in="${playerInstance.matches.match}" var="match">
            <li><g:link controller="match" action="show" id="${match.id}">${match.encodeAsHTML()}</g:link></li>
        </g:each>
    </ul>
</div>
</body>
</html>
